<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class rtrw extends CI_Controller {
	public function __construct() 
	{       
            parent::__construct();
            $this->load->library('session');
            $this->load->helper('url');
            $this->load->library('form_validation');
            $this->load->library("pagination");     
            $this->load->model('desa_model');
            
        }
        
        public function index() {
            $data["list_kec"]= $this->desa_model->get_kec_poly(0);
            
            $this->load->view('rtrw/header');
            $this->load->view('rtrw/petartrw',$data);
            $this->load->view('rtrw/footer');
        }

        
        public function strategis() {
            $data["list_kec"]= $this->desa_model->get_kec_poly(0);
            
            $this->load->view('rtrw/header');
            $this->load->view('rtrw/petartrw_strategis',$data);
            $this->load->view('rtrw/footer');
        }

        public function budidaya() {
            $data["list_kec"]= $this->desa_model->get_kec_poly(0);
            
            $this->load->view('rtrw/header');
            $this->load->view('rtrw/petartrw_budidaya',$data);
            $this->load->view('rtrw/footer');
        }
        
        public function lindung() {
            $data["list_kec"]= $this->desa_model->get_kec_poly(0);
            
            $this->load->view('rtrw/header');
            $this->load->view('rtrw/petartrw_lindung',$data);
            $this->load->view('rtrw/footer');
        }
        
        public function get_polygon_desa($id_kec=""){
            header('Access-Control-Allow-Origin: *');
            $desa_list = array();
            $kec = $this->desa_model->get_kec_kode($id_kec)->result_array();
            if($kec)
            $kode_kec = $kec[0]["KEC_KODE"];
            else $kode_kec = 0;
            
            $p= $this->desa_model->get_desa_poly($kode_kec);

            foreach($p->result_array() as $r){//data loc buat marker
                $data_way = explode(" ", $r["DESA_GEOMETRY"]);
                $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
                $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

                $point_desa_list = array();
                for($i=1;$i<count($data_way);$i++)
                {
                    $latlng = explode(",", $data_way[$i]);
                    $lat = $latlng[0];
                    $lng = $latlng[1];

                    if($i==0) {//echo $latlng[1];
                        $temp1 = explode(">", $latlng[0]);
                        $lat = $temp1[4];
                    }

                    if($i==count($data_way)-1) {//echo $latlng[1];
                        $temp = explode("<", $latlng[1]);
                        $lng = $temp[0]; 
                    }

                    $data_latlng = array(
                        'LAT' => $lng,
                        'LNG' => $lat
                    );

                    array_push($point_desa_list, $data_latlng);
                }

                $data_per_desa = array(
                    'DESA_ID' => $r["DESA_ID"],
                    'DESA_NAMA' => $r["DESA_NAMA"],
                    'DESA_KODE' => $r["DESA_KODE"],
                    'KEC_KODE' => $r["KEC_KODE"],
                    'KECAMATAN_NAMA' => $r["KECAMATAN"],
                    'KAB_KOTA' => $r["KAB_KOTA"],
                    'PROPINSI' => $r["PROPINSI"],
                    'DESA_POINT_LIST' => $point_desa_list,
                    'COLOR' => $color
                );
                array_push($desa_list, $data_per_desa); 
            }

            echo json_encode(array('Desa' => $desa_list));
            exit;
        }
        
        public function get_polygon_kec($id_kec=0){
            
            header('Access-Control-Allow-Origin: *');
            $kec_list = array();
            $kec = $this->desa_model->get_kec_kode($id_kec)->result_array();
            if($kec)
            $kode_kec = $kec[0]["KEC_KODE"];
            else $kode_kec = 0;
            
            $p= $this->desa_model->get_kec_poly($kode_kec);

            foreach($p->result_array() as $r){//data loc buat marker
                $data_way = explode(" ", $r["KEC_GEOMETRY"]);
                $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
                $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

                $point_kec_list = array();
                for($i=1;$i<count($data_way);$i++)
                {
                    $latlng = explode(",", $data_way[$i]);
                    $lat = $latlng[0];
                    $lng = $latlng[1];

                    if($i==0) {//echo $latlng[1];
                        $temp1 = explode(">", $latlng[0]);
                        $lat = $temp1[4];
                    }

                    if($i==count($data_way)-1) {//echo $latlng[1];
                        $temp = explode("<", $latlng[1]);
                        $lng = $temp[0]; 
                    }

                    $data_latlng = array(
                        'LAT' => $lng,
                        'LNG' => $lat
                    );

                    array_push($point_kec_list, $data_latlng);
                }

                $data_per_kec = array(
                    'KEC_ID' => $r["KEC_ID"],
                    'KEC_KODE' => $r["KEC_KODE"],
                    'KEC_NAMA' => $r["KEC_NAMA"],
                    'KAB_NAMA' => $r["KAB_NAMA"],
                    'PROV_NAMA' => $r["PROV_NAMA"],
                    'KEC_POINT_LIST' => $point_kec_list,
                    'COLOR' => $color
                );
                array_push($kec_list, $data_per_kec); 
            }

            echo json_encode(array('Desa' => $kec_list));
            exit;
        }
    }

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */